﻿package com.housediyadmin.view
{
	
	/**
	 * 
	 * @author yanghanbing
	 * 2010/11/24
	 */	

	import com.housediyadmin.events.SelectedFittingEve;
	import com.housediyadmin.events.ShowDetailEve;
	import com.housediyadmin.events.brandEvents.AddBrandEvent;
	import com.housediyadmin.events.brandEvents.DeleteBrandByIDEvent;
	import com.housediyadmin.events.brandEvents.EditBrandEvent;
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.events.fittingEvents.DeleteFittingByIDEvent;
	import com.housediyadmin.events.fittingEvents.EditFittingEvent;
	import com.housediyadmin.events.fittingEvents.GetFittingEvent;
	import com.housediyadmin.events.goodsEvents.AddGoodsEvent;
	import com.housediyadmin.events.goodsEvents.DeleteGoodsByIDsEvent;
	import com.housediyadmin.events.goodsEvents.GetAllGoodsEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByBrandIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.treeEvents.DeleteGoodsLevelEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.view.components.EditBrandView;
	import com.housediyadmin.view.components.EditFittingView;
	import com.housediyadmin.view.titlewindow.EditGoodsView;
	import com.housediyadmin.vo.Brand;
	import com.housediyadmin.vo.Fitting;
	import com.housediyadmin.vo.Goods;
	
	import flash.events.MouseEvent;
	
	import flashx.textLayout.events.ModelChange;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ListEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class GoodsAdminMeditor extends Mediator
	{
		[Inject]
		public var view:GoodsAdminView;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		[Inject]
		public var fittingModel:FittingModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		override public function onRegister():void
		{
			view.goodsShow.addGoodsBtn.addEventListener(MouseEvent.CLICK, popUpAddGoodsView);
			dispatch(new GetAllGoodsEvent(GetAllGoodsEvent.GET_ALL_GOODS, "door", null));
			BindingUtils.bindProperty(view.goodsShow.goodsList, "dataProvider", goodsModel, "goodsArray");
			dispatch(new GetFittingEvent(GetFittingEvent.GET_ALL_FITTINGS));
			//tree
		//	view.goodsTree.tree.addEventListener(ListEvent.ITEM_CLICK, selectTreeNode);
		//	view.goodsTree.deleteBtn.addEventListener(MouseEvent.CLICK,deleteTreeNode);
			
			
			//fttting
			/*BindingUtils.bindProperty(view.fittingCB,"dataProvider",fittingModel,"fittingsArray");
			dispatch(new GetFittingEvent(GetFittingEvent.GET_ALL_FITTINGS));
			view.fittingCB.addEventListener(ListEvent.CHANGE, showFittingDetail);
//			view.fittingCB.selectedIndex=1;
					
			view.addFittingBtn.addEventListener(MouseEvent.CLICK,addFittingPopup);
			view.editFittingBtn.addEventListener(MouseEvent.CLICK,editFittingPopup);
			view.deleteFittingBtn.addEventListener(MouseEvent.CLICK,deleteFittingAlert);
			
			//brands
			BindingUtils.bindProperty(view.brandCB,"dataProvider",brandModel,"brandsArray");
			
			
			
			view.brandCB.addEventListener(ListEvent.CHANGE, showBrandDetail);
			view.brandCB.selectedIndex=0;
			
			view.addBrandBtn.addEventListener(MouseEvent.CLICK,addBrandPopup);
			view.editBrandBtn.addEventListener(MouseEvent.CLICK,editBrandPopup);
			view.deleteBrandBtn.addEventListener(MouseEvent.CLICK,deleteBrandAlert);*/
			
			
			//goods
			/*view.addGoodsBtn.addEventListener(MouseEvent.CLICK,addGoodsPopup);
			view.deleteSeveralGoodsBtn.addEventListener(MouseEvent.CLICK,deleteSeveralGoods);*/
			//view.goodsDG.addEventListener(ListEvent.CHANGE,selectGoodsDG);
			
			/*dispatch(new GetBrandEvent(GetBrandEvent.GET_BRANDS_MODEL));*/
		}
		
		//tree
		
		private function popUpAddGoodsView(event:MouseEvent):void
		{
			var goodsEditPopUp:EditGoodsView = new EditGoodsView();
			PopUpManager.addPopUp(goodsEditPopUp, view, true);
			mediatorMap.createMediator(goodsEditPopUp);
			PopUpManager.centerPopUp(goodsEditPopUp);
			goodsEditPopUp.addEventListener(CloseEvent.CLOSE, closeHandler);
		}
		
		private function closeHandler(event:CloseEvent):void
		{
			//PopUpManager.removePopUp(
		}
		
		private function selectTreeNode(e:ListEvent):void
		{
			var selectedItem:XML=XML(e.target.selectedItem);
			goodsTreeModel.selectedNode=selectedItem;
			
			var treeNodeIDs:XMLList=goodsTreeModel.selectedNode..@typeID;
			var typeIdArray:Array=new Array();
			for each(var xml:XML in treeNodeIDs)
			{
				var typeID:int=xml.toXMLString();
				typeIdArray.push(typeID);
			}
			dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID,typeIdArray));
			//BindingUtils.bindProperty(view.goodsDG,"dataProvider",goodsModel,"goodsArray");
		}
		
		private function deleteTreeNode(e:MouseEvent):void
		{
			
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除该结点吗？","",3,view,deleteTreeNodeAlert);
		}
		private function deleteTreeNodeAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				//下面有结点的，不给删 
				var selectedTreeNode:XML=goodsTreeModel.selectedNode;
				var treeToDelID:int=selectedTreeNode.@typeID;
				
				if(selectedTreeNode.children().length()!=0)
				{
					Alert.show("还有下级结点未删除。删除失败！");
					return;
				}
				
				//结点下有goods的，不给删 
				
				if(selectedTreeNode.@typeLevel==3)
				{
					
					for each(var goods:Goods in goodsModel.goodsArray)
					{
						/*if(goods.id==treeToDelID)
						{
							Alert.show("树节点下还有商品未删除。删除失败！");
							return;
						}*/
					}
				}
				dispatch(new DeleteGoodsLevelEvent(DeleteGoodsLevelEvent.DELETE_GOODS_LEVEL,treeToDelID));
			}
		}
		//fitting
		private function showFittingDetail(e:ListEvent):void
		{
			fittingModel.selectedFitting=e.target.selectedItem;
			//view.describeFittingLbl.text=fittingModel.selectedFitting.fittingsDesc;
		}
		
		private var selectedFittingIndex:int;		
 
		
		private function addFittingPopup(e:MouseEvent):void
		{
			var popup:EditFittingView = new EditFittingView();
			popup.type="ADD";
			selectedFittingIndex=fittingModel.fittingsArray.length;
			mediatorMap.createMediator(popup);
			this.addContextListener(ShowDetailEve.FittingDetail,ShowFittingDetailAfterPopup);
			PopUpManager.addPopUp( popup, contextView,true ); 
			PopUpManager.centerPopUp(popup);
		}

		private function editFittingPopup(e:MouseEvent):void
		{
			var popup:EditFittingView = new EditFittingView();
			if(fittingModel.selectedFitting)
			{
				popup.type="EDIT";
				//selectedFittingIndex=view.fittingCB.selectedIndex;
				PopUpManager.addPopUp( popup, contextView ); 
				PopUpManager.centerPopUp(popup);
				mediatorMap.createMediator(popup);
				this.addContextListener(ShowDetailEve.FittingDetail,ShowFittingDetailAfterPopup);
			}
			else
			{
				Alert.show("请先选择编辑对象！");
			}
		}
		
		private function ShowFittingDetailAfterPopup(e:ShowDetailEve):void
		{
			/*view.fittingCB.selectedIndex=selectedFittingIndex;
			view.describeFittingLbl.text=e.obj.fittingsDesc;*/
			fittingModel.selectedFitting=e.obj as Fitting;
		}
		
		
		
	/*	private function deleteFittingAlert(e:MouseEvent):void
		{
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除商品配套信息吗？","",3,view,deleteFitting);
		}*/
		
		/*private function deleteFitting(e:CloseEvent):void
		{
			
			if(e.detail == Alert.YES)
			{
				if(!fittingModel.selectedFitting)
				{
					Alert.show("请选择删除对象！");
				}
				else
				{
					dispatch(new DeleteFittingByIDEvent
						(DeleteFittingByIDEvent.DELETE_FITTING_BY_ID,fittingModel.selectedFitting.fittingID));
					
					Alert.show("删除成功！");
	//				view.fittingCB.selectedIndex=-1;
					//view.describeFittingLbl.text="";
					fittingModel.selectedFitting=null;
				}
			}
		}
		*/
		//brands
		private function showBrandDetail(e:ListEvent):void
		{
			brandModel.selectedBrand=e.target.selectedItem;
			//view.describeBrandLbl.text=brandModel.selectedBrand.brandIntroduce;
			//view.brandImg.source="http://10.60.1.120/HouseDiyAdmin/src/assets/brandsassets/"+brandModel.selectedBrand.brandPicSrc;
		}
		
		private var selectedBrandIndex:int;
		private function addBrandPopup(e:MouseEvent):void
		{
			var popup:EditBrandView = new EditBrandView();
			popup.type="ADD";
/*			selectedBrandIndex=brandModel.brandsArray.length;*/
			mediatorMap.createMediator(popup);
			PopUpManager.addPopUp( popup, contextView,true ); 
			PopUpManager.centerPopUp(popup);
			this.addContextListener(ShowDetailEve.BrandDetail,ShowBrandDetailAfterPopup);
		}
		
		private function editBrandPopup(e:MouseEvent):void
		{
			if(brandModel.selectedBrand)
			{
				if(brandModel.selectedBrand.brandName=="其它")
				{
					Alert.show("该项不可修改！");
				}
				else
				{
					var popup:EditBrandView = new EditBrandView();
					popup.type="EDIT";
					//selectedBrandIndex=view.brandCB.selectedIndex;
					mediatorMap.createMediator(popup);
					PopUpManager.addPopUp( popup, contextView,true ); 
					PopUpManager.centerPopUp(popup);
					this.addContextListener(ShowDetailEve.BrandDetail,ShowBrandDetailAfterPopup);
				}
			}
			else
			{
				Alert.show("请选择编辑对象！");
			}
		}
		
		private function ShowBrandDetailAfterPopup(e:ShowDetailEve):void
		{
			brandModel.selectedBrand=e.obj as Brand;
			//view.brandCB.selectedIndex=selectedBrandIndex;
			//view.describeBrandLbl.text=Brand(e.obj).brandIntroduce;
			//view.brandImg.source=Brand(e.obj).brandPicSrc;
		}
		
		private function deleteBrandAlert(e:MouseEvent):void
		{
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除品牌信息吗？","",3,view,deleteBrand);
		}
		
		private function deleteBrand(e:CloseEvent):void
		{
			if(e.detail == Alert.YES)
			{
				if(!brandModel.selectedBrand)
				{
					Alert.show("请选择删除对象！");
				}
				else
				{
					if(brandModel.selectedBrand.brandName=="其它")
					{
						Alert.show("删除失败!请不要删除'其它'!");
						return;
					}
					
				/*	dispatch(new DeleteBrandByIDEvent
						(DeleteBrandByIDEvent.DELETE_BRAND_BY_ID,brandModel.selectedBrand.brandID));
					*/
					Alert.show("删除成功！");
					brandModel.selectedBrand=null;
					//view.brandCB.selectedIndex=-1;
					//view.describeBrandLbl.text="";
					//view.brandImg.source="";
				}
			}
		}
		
		//goods
		private function addGoodsPopup(e:MouseEvent):void
		{
			if(!goodsTreeModel.selectedNode)
			{
				Alert.show("请先选中树节点！添加失败！");
				return;
			}
			
			var typeLevel:int=goodsTreeModel.selectedNode.@typeLevel;
			if(typeLevel==3)
			{
				var popup:EditGoodsView = new EditGoodsView();
				popup.type="ADD";
//				selectedBrandIndex=brandModel.brandsArray.length;
				mediatorMap.createMediator(popup);
				PopUpManager.addPopUp( popup, contextView,true ); 
				PopUpManager.centerPopUp(popup);
//				this.addContextListener(ShowDetailEve.BrandDetail,ShowBrandDetailAfterPopup);
				
			}
			else
			{
				Alert.show("添加失败！只有三级目录才能添加商品。");
			}
		}
		
	
		
		private function deleteSeveralGoods(e:MouseEvent):void
		{
			Alert.yesLabel="是";
			Alert.noLabel="否";
			Alert.cancelLabel="取消";
			Alert.show("删除商品信息吗？","",3,view,deleteAlert);
		}
		private function deleteAlert(e:CloseEvent):void
		{
			if(e.detail==Alert.YES)
			{
				if(goodsModel.selectedGoodsArr.length==0)
				{
					Alert.show("请先选择批量删除对象！");
					return;
				}
				var goodsIds:Array=new Array();
				for each(var goods:Goods in goodsModel.selectedGoodsArr)
				{
					//goods.isSelected=false;
					//goodsIds.push(goods.goodsId);
				}
				
				var typeIds:Array=new Array();
				var typeID:int=goodsTreeModel.selectedNode.@typeID;
				typeIds.push(typeID);
				dispatch(new DeleteGoodsByIDsEvent(DeleteGoodsByIDsEvent.DELETE_GOODS_BY_IDS,goodsIds,typeIds));
			}
		}
		
		private function selectGoodsDG(e:ListEvent):void
		{
			goodsModel.selectedGoods=e.target.selectedItem;
		}
	}
}